﻿using System;
using System.IO;
using System.Windows.Forms;

using EShop.BLL;
using EShop.Model;
using EShop.Common;

namespace EShop.Client
{
    public partial class frmUserLogin : frmBase
    {
        public bool IsLogoff = false;

        public frmUserLogin()
        {
            InitializeComponent();

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if(sender == null)
            {
                if (!chkAutoLogin.Checked)
                    return;
            }

            string strEmployeeID = SystemLibrary.SQLQuoteName(txtEmployeeID.Text.Trim());
            string strEmployeePWD = SystemLibrary.SQLQuoteName(txtEmployeePWD.Text.Trim());
            if (strEmployeeID.Length == 0)
            {
                lblLoginMsg.Text = "请输入登录工号！";
                panel1.Visible = true;
                txtEmployeeID.Focus();
                return;
            }
            if (strEmployeePWD.Length == 0)
            {
                lblLoginMsg.Text = "请输入登录密码！";
                panel1.Visible = true;
                txtEmployeePWD.Focus();
                return;
            }

            try
            {
                EmployeeBLL oEmployeeBLL = new EmployeeBLL();
                EmployeeInfo oInfo = oEmployeeBLL.EmployeeLogined(strEmployeeID, strEmployeePWD);
                //if (oEshopConfigInfo == null) oEshopConfigInfo = Serial.ReadConfig();

                //if (se == null) SetSkin();
                //se.SkinFile = oEshopConfigInfo.Skin;

                if (oInfo != null)
                {

                    oEshopConfigInfo.LoginID = strEmployeeID;
                    oEshopConfigInfo.EmployeeFullName = oInfo.FullName;
                    oEshopConfigInfo.Permission = oInfo.EmployeePermission;
                    oEshopConfigInfo.LastLoginTime = oInfo.LastLoginDatatime.ToString();
                    oEshopConfigInfo.AutoLogin = chkAutoLogin.Checked;
                    if (chkSavePWD.Checked)
                        oEshopConfigInfo.LoginPWD = strEmployeePWD;

                    oShopConfigInfo = oEshopConfigInfo;
                    //保存配置
                    Serial.SaveConfig(oShopConfigInfo);

                    frmMain mainForm = new frmMain();
                    this.Hide();
                    mainForm.Show();
                }
                else
                {
                    lblLoginMsg.Text = "登录失败，请检查登录工号和密码！";
                    panel1.Visible = true;
                    //txtEmployeeID.Text = "";
                    txtEmployeePWD.Text = "";
                    txtEmployeeID.Focus();
                }
            }
            catch (Exception ex)
            {
                LogWrite(ex);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private EshopConfigInfo oEshopConfigInfo = new EshopConfigInfo();

        private void frmUserLogin_Load(object sender, EventArgs e)
        {
            oEshopConfigInfo = Serial.ReadConfig();
            if (se == null) SetSkin();
            se.SkinFile = oEshopConfigInfo.Skin;
            MessageBox.Show(oEshopConfigInfo.Skin);
            if (oEshopConfigInfo != null)
            {
                txtEmployeeID.Text = oEshopConfigInfo.LoginID;
                txtEmployeePWD.Text = oEshopConfigInfo.LoginPWD;
                chkSavePWD.Checked = string.IsNullOrEmpty(oEshopConfigInfo.LoginPWD) ? false : true;
                chkAutoLogin.Checked = oEshopConfigInfo.AutoLogin;
                if (oEshopConfigInfo.AutoLogin&&IsLogoff==false)
                    AutoLogin_timer.Enabled = true;
            }
        }

        private void AutoLogin_timer_Tick(object sender, EventArgs e)
        {
            AutoLogin_timer.Enabled = false;
            btnLogin_Click(null, null);
        }
    }
}
